package com.reviewit.dao;

import org.bson.types.ObjectId;

import com.google.code.morphia.Morphia;
import com.google.code.morphia.dao.BasicDAO;
import com.google.code.morphia.query.Query;
import com.mongodb.Mongo;
import com.reviewit.dao.mongodb.MongoUtil;
import com.reviewit.model.User;

public class UserDao extends BasicDAO<User, ObjectId> {
	private static UserDao INSTANCE = new UserDao(MongoUtil.getMongo(),
			MongoUtil.getMorphia(), MongoUtil.getDbName());

	protected UserDao(Mongo mongo, Morphia morphia, String dbName) {
		super(mongo, morphia, dbName);
	}

	public static UserDao getInstance() {
		return INSTANCE;
	}

	public User login(String email, String senha) {
		Query<User> query = MongoUtil.getDB().createQuery(User.class);
		query.and(query.criteria("email").equal(email),
				query.criteria("password").equal(senha));
		return findOne(query);
	}

	public User existeEmail(String email) {
		Query<User> query = MongoUtil.getDB().createQuery(User.class);
		query.and(query.criteria("email").equal(email));
		return findOne(query);
	}
}